package org.dromara.system.controller;

import java.util.List;

import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.TCustomVo;
import org.dromara.system.domain.bo.TCustomBo;
import org.dromara.system.service.ITCustomService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 客户信息
 *
 * @author Lion Li
 * @date 2024-10-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/custom")
public class TCustomController extends BaseController {

    private final ITCustomService tCustomService;

    /**
     * 查询客户信息列表
     */
    @GetMapping("/list")
    public TableDataInfo<TCustomVo> list(TCustomBo bo, PageQuery pageQuery) {
        return tCustomService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出客户信息列表
     */
    @SaCheckPermission("system:custom:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TCustomBo bo, HttpServletResponse response) {
        List<TCustomVo> list = tCustomService.queryList(bo);
        ExcelUtil.exportExcel(list, "客户信息", TCustomVo.class, response);
    }

    /**
     * 获取客户信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:custom:query")
    @GetMapping("/{id}")
    public R<TCustomVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer id) {
        return R.ok(tCustomService.queryById(id));
    }

    /**
     * 新增客户信息
     */
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<TCustomBo> add(@Validated(AddGroup.class) @RequestBody TCustomBo bo) {
        if(bo.getConnectUserId() == null){
            bo.setConnectUserId(StpUtil.getLoginIdAsString());
        }
        Boolean insert = tCustomService.insertByBo(bo);
        if (insert) {
            return R.ok(bo);
        } else {
            return R.fail("新增失败");
        }

    }

    /**
     * 修改客户信息
     */
    @SaCheckPermission("system:custom:edit")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TCustomBo bo) {
        return toAjax(tCustomService.updateByBo(bo));
    }

    /**
     * 删除客户信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:custom:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] ids) {
        return toAjax(tCustomService.deleteWithValidByIds(List.of(ids), true));
    }
}
